const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
const tabService = require("../../libs/tabService");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sid:1,
        sname:'',
        brand_id:1,
        series_id:0,
        color_id:0,
        motor_id:0,
        cabin_id:0,
        box_id:0,
        page:1,
        more:1,
        isTop:0,
    
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this ;
        let d =   app.globalData;
        var open =  wx.getStorageSync('open');
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })   
        that.setData({
            platform:d.platform,    
            open:open,
             width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    Index(page){
        let that = this;
        var open =  wx.getStorageSync('open');
        var data = [];
        data['user_id']= open.user_id;
        data['page']= page;
        var url = 'clear/getIndexs';

      app.request(url,data,'get').then(res=>{ 
        console.log('clear getIndex'); 
         console.log(res);
     
         //this.initChart(res.pie);
         that.setData({
             cabin:res.cabin,
             motor:res.motor,
             brand:res.brand,
             color:res.color,
             series:res.series,
             list:res.list,
             box:res.box,
             adimg:res.adimg,
             count:res.count,
             title:res.title,
             total:res.total,
             more:res.more,
             shareTitle:res.shareTitle,
             shareLogo:res.shareLogo,
             shareIndex:res.shareIndex,    
            
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })

      },
      getList(page){
        console.log(page);
        let that = this;
        var open =  wx.getStorageSync('open');
        var data = [];
        data['user_id']= open.user_id;
        data['page']= page;
        var url = 'clear/getList';

      app.request(url,data,'get').then(res=>{ 
        console.log('clear getList'); 
         console.log(res);
         var list = that.data.list;
         list = list.concat(res.list)
         //this.initChart(res.pie);
         that.setData({
            list:list,           
            count:res.count,
            more:res.more
     
 
            
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })
      },
      getHeight:function(e){
        var winWid = wx.getSystemInfoSync().windowWidth;//获取当前屏幕的宽度
        var imgh = e.detail.height;//图片高度
        var imgw = e.detail.width;
        var sH = winWid * imgh / imgw + "px"
        this.setData({
            swiperH: sH//设置高度
        })
    },
    toTop(e){
        this.setData({
            isTop:0
        })
        wx.pageScrollTo({
            scrollTop:0,
            duration:200
        })

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {    
       
        //this.getIndex();
       this.Index(1);
        var role = wx.getStorageSync('role');
       console.log(role);
       var  tab = wx.getStorageSync('tabBar');  
       tabService.updateTabArray(this,tab);
       var tabIndex = wx.getStorageSync('tabIndex')
        tabService.updateIndex(this, tabIndex);
      },
      update(role) {
        tabService.updateRole(this, role);
      },
      goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        app.goto(url,index);
    },
    goBack:function(){
        wx.navigateBack();
      },
      goIndex:function(){
        wx.reLaunch({
          url: '../index/index',
        })
      },
      getSeries(e){
          console.log(e);
          var d = e.currentTarget.dataset;
          var v= this.data;
          var brand = v.brand;
          var series = this.data.series; 
          var id = d.id;
          var name = d.name;        
              for (let i = 0; i < brand.length; ++i) {
                  console.log(brand);                
                  var ids = brand[i].id;
                  console.log('ids='+ids+"id=>"+id)
                  var len = 0;
                  var scrollTop = 0;
                  if(id == ids){
                    len = i;
                    scrollTop = len*69;
                    this.setData({                     
                      scrollTop:scrollTop
                    })
                  }
                  
                  //console.log(scrollTop);
                  if ("siv"+ids === v.toView) {
                      //scrollTop = i*90;
                  console.log(scrollTop);
                    this.setData({
                      toView: "siv"+ids,
                     
                    })
                    break
                  }
                 
                }
          

      
          var data = [];
          data['id']= id;
          var url = 'clear/getSeries';
        let that = this;
        app.request(url,data,'get').then(res=>{ 
          console.log('clear index getSeries'); 
           console.log(res);
           var len = res.len;
           var series = res.series;
           var height = 74+42*len;
          that.setData({
              brand_id:id,
              brand_name:name,
              showSeries:1,
              needH:height,
              series:series
          })
        });
          
      
          
      },
      setSeries(e){
          console.log(e);
          var d = e.currentTarget.dataset;
          var id = d.id;
          var name = d.name;
          let v = this.data; 
          if(id == 0){
            name = v.brand_name+"全系";
        }  
          this.search(v.brand_id,id,v.color_id,v.motor_id,v.cabin_id,v.box_id);
          this.setData({
            showSeries:0,
            series_id:id,
            series_name:name,
           // needH:height
        })
      },
      setSearch(e){
          console.log(e);
          var d = e.currentTarget.dataset;
          var id = d.id;
          //var name = d.name
          this.setData({
              sid:id,
          })
      },
      setColor(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
    
        let v = this.data;     
        this.search(v.brand_id,v.series_id,id,v.motor_id,v.cabin_id,v.box_id);
        this.setData({
            color_id:id,
            color_name:name
        })
      },
      setMotor(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
        let v = this.data;     
        this.search(v.brand_id,v.series_id,v.color_id,id,v.cabin_id,v.box_id);
        this.setData({
            motor_id:id,
            motor_name:name
        })
      },
      setCabin(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
        let v = this.data;     
        this.search(v.brand_id,v.series_id,v.color_id,v.motor_id,id,v.box_id);
        this.setData({
            cabin_id:id,
            cabin_name:name
        })
      },
      setBox(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
        let v = this.data;     
        this.search(v.brand_id,v.series_id,v.color_id,v.motor_id,v.cabin_id,id);
        this.setData({
            box_id:id,
            box_name:name
        })
      },
      delBox(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,d.color_id,d.motor_id,d.cabin_id,0);
        this.setData({
            box_id:0,
            box_name:''
        })
      },
      delCabin(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,d.color_id,d.motor_id,0,d.box_id);
        this.setData({
            cabin_id:'',
            cabin_name:''
        })
      },
      delMotor(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,d.color_id,0,d.cabin_id,d.box_id);
        this.setData({
            motor_id:'',
            motor_name:''
        })
      },
      delColor(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,0,d.motor_id,d.cabin_id,d.box_id);
        this.setData({
            color_id:'',
            color_name:''
        })
      },
      delSeries(){
        let d = this.data;          
        this.search(0,0,d.color_id,d.motor_id,d.cabin_id,d.box_id);
        this.setData({
            series_id:'',
            brand_id:0,
            brand_name:'',
            series_name:''
        })
      },
      toSearch(e){
        var value = e.detail.value;
        let that = this;
        //var open =  wx.getStorageSync('open');
        var data = [];       
        data['key']= value;
        var url = 'clear/toSearch';
        app.request(url,data,'get').then(res=>{ 
            console.log('customer getIndex'); 
             console.log(res);
         
             //this.initChart(res.pie);
             that.setData({   
                 'list':res.list,  
                 'count':res.count,
                 more:0       
                 
             });
    
           //  that.wxUpdate(); //that.wxLogIn();  
          })
      },
      search(brand_id,series_id,color_id,motor_id,cabin_id,box_id){
        let that = this;
        //var open =  wx.getStorageSync('open');
        var data = [];
        data['brand_id']= brand_id;
        data['series_id']= series_id;
        data['color_id']= color_id;
        data['motor_id']= motor_id;
        data['cabin_id']= cabin_id;
        data['box_id']= box_id;
        var url = 'clear/search';

      app.request(url,data,'get').then(res=>{ 
        console.log('customer getIndex'); 
         console.log(res);
     
         //this.initChart(res.pie);
         that.setData({   
             'list':res.list,  
             'count':res.count,
             more:0         
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
    onPageScroll(e){
       // console.log(e);
       var scrollTop = e.scrollTop;
       console.log(scrollTop)
       var isTop=0;
       if(scrollTop>300){
        isTop=1;
       }
       this.setData({
        isTop:isTop
    }) 
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('more');
        var d = this.data;
        var page = d.page;
        var more = d.more;
        if(more){
            //console.log('more');
            page = page+1;
            console.log(page);
            this.setData({
                page:page
            })
            this.getList(page);
        }else{
            //console.log('no more');
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareTimeline(e){
        console.log(e);
        var title = this.data.shareTitle;
        var shareLogo = this.data.shareLogo;
        var open = wx.getStorageSync('open');
        var store_id = wx.getStorageSync('store_id');
        var parent_id=open.user_id;
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         return{
           title: title,                 
           query: 'share=6&parent_id='+parent_id+'&store_id='+store_id,        
           imageUrl:shareLogo      
           }
       },
    onShareAppMessage(e) {
        var open = wx.getStorageSync('open');
        var parent_id=open.user_id;
        var store_id = wx.getStorageSync('store_id');
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareIndex;
   　  　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?share=6&parent_id='+parent_id+'&store_id='+store_id,   
   　　　　imageUrl: shareLogo 
   　　　
   　　}
   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?share=6&parent_id='+parent_id+'&store_id='+store_id;
   　　}
   console.log(shareObj);
   　　// 返回shareObj
   　　return shareObj;
    }
})